package com.powernode.driver.controller;

import com.powernode.common.annotation.PowerLogin;
import com.powernode.common.result.Result;
import com.powernode.common.util.AuthContextHolder;
import com.powernode.driver.service.OrderService;
import com.powernode.model.form.map.CalculateDrivingLineForm;
import com.powernode.model.form.order.OrderFeeForm;
import com.powernode.model.form.order.StartDriveForm;
import com.powernode.model.form.order.UpdateOrderCartForm;
import com.powernode.model.vo.map.DrivingLineVo;
import com.powernode.model.vo.order.CurrentOrderInfoVo;
import com.powernode.model.vo.order.NewOrderDataVo;
import com.powernode.model.vo.order.OrderInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "订单API接口管理")
@RestController
@RequestMapping("/order")
@SuppressWarnings({"unchecked", "rawtypes"})
public class OrderController {

    @Resource
    private OrderService orderService;



    @Operation(summary = "配送员查询当前进行中的订单")
    @GetMapping("getOrderStatus/{orderId}")
    public Result<Integer> queryOrder(@PathVariable Long orderId) {
        return Result.ok(orderService.queryOrderStatus(orderId));
    }

    @Operation(summary = "配送员查询符合的订单")
    @GetMapping("findNewOrderQueueData")
    @PowerLogin
    public Result<List<NewOrderDataVo>> findNewOrderQueueData() {
        Long driverId = AuthContextHolder.getUserId();
        return Result.ok(orderService.findNewOrderQueueData(driverId));
    }


    @Operation(summary = "查看当前配送员是否有配送中的订单")
    @PowerLogin
    @GetMapping("/searchDriverCurrentOrder")
    public Result<CurrentOrderInfoVo> searchDriverCurrentOrder() {
        Long driverId = AuthContextHolder.getUserId();
        return Result.ok(orderService.searchDriverCurrentOrder(driverId));
    }

    @Operation(summary = "配送员抢单")
    @PowerLogin
    @GetMapping("/robNewOrder/{orderId}")
    public Result<Boolean> robNewOrder(@PathVariable Long orderId) {
        Long userId = AuthContextHolder.getUserId();

        return Result.ok(orderService.robNewOrder(userId, orderId));
    }


    @Operation(summary = "根据id获取配送员的订单")
    @PowerLogin
    @GetMapping("/getOrderInfo/{orderId}")
    public Result<OrderInfoVo> getOrderInfo(@PathVariable Long orderId) {
        Long driverId = AuthContextHolder.getUserId();
        return Result.ok(orderService.getOrderInfo(orderId, driverId));
    }


    @Operation(summary = "查看最佳路线")
    @PowerLogin
    @PostMapping("/calculateDrivingLine")
    public Result<DrivingLineVo> calculateDrivingLine(@RequestBody CalculateDrivingLineForm calculateDrivingLineForm) {
        return Result.ok(orderService.calculateDrivingLine(calculateDrivingLineForm));
    }

    @Operation(summary = "配送员到达指定位置")
    @PowerLogin
    @GetMapping("/driverArrivedStartLocation/{orderId}")
    public Result<Boolean> driverArrivedStartLocation(@PathVariable Long orderId) {
        Long driverId = AuthContextHolder.getUserId();
        return Result.ok(orderService.driverArrivedStartLocation(orderId, driverId));
    }

    @Operation(summary = "更新配送员车辆信息")
    @PowerLogin
    @PostMapping("/updateOrderCart")
    public Result<Boolean> updateOrderCart(@RequestBody UpdateOrderCartForm updateOrderCartForm) {
        Long userId = AuthContextHolder.getUserId();
        updateOrderCartForm.setDriverId(userId);

        return Result.ok(orderService.updateDriverCarInfo(updateOrderCartForm));
    }


    @Operation(summary = "配送员开始配送")
    @PowerLogin
    @PostMapping("/startDrive")
    public Result<Boolean> startDrive(@RequestBody StartDriveForm startDriveForm) {
        Long userId = AuthContextHolder.getUserId();
        startDriveForm.setDriverId(userId);
        return Result.ok(orderService.startDrive(startDriveForm));
    }


    @Operation(summary = "配送员到达配送起始地点")
    @PowerLogin
    @GetMapping("/driverArriveStartLocation/{orderId}")
    public Result<Boolean> driverArriveStartLocation(@PathVariable Long orderId) {
        Long driverId = AuthContextHolder.getUserId();
        return Result.ok(orderService.driverArriveStartLocation(orderId, driverId));
    }


    @Operation(summary = "结束配送服务更新订单账单")
    @PowerLogin
    @PostMapping("/endDrive")
    public Result<Boolean> endDrive(@RequestBody OrderFeeForm orderFeeForm) {
        Long driverId = AuthContextHolder.getUserId();
        orderFeeForm.setDriverId(driverId);
        return Result.ok(orderService.endDrive(orderFeeForm));
    }
}

